import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue';
import LoginView from '../views/LoginView';
import RegisterView from '../views/RegisterView';
import NotFoundView from '../views/NotFoundView';
import UserProfileView from '../views/UserProfileView';
import BuildingManageView from '@/views/BuildingManageView.vue';
import AddBuilding from '@/views/BuildingView/AddBuilding.vue';
import UpdateBuilding from '@/views/BuildingView/UpdateBuilding.vue';
import DormManageView from '@/views/DormManageView.vue';
import AddDorm from '@/views/DormManage/AddDorm.vue';
import UpdateDorm from '@/views/DormManage/UpdateDorm.vue';
import StudentManageView from '@/views/StudentManageView.vue';
import RepairManageView from '@/views/RepairManageView.vue';
import ProcessedView from '@/views/RepairManage/ProcessedView.vue';
import PendingView from '@/views/RepairManage/PendingView.vue';
import RepairPending from '@/views/StudentView/RepairPending.vue';
import RepairProcessed from '@/views/StudentView/RepairProcessed.vue';
import StudentRepairView from '@/views/StudentRepairView.vue';
import RepairRevoke from '@/views/StudentView/RepairRevoke.vue';
import EventsView from '@/views/EventsView.vue';
import EventsEditorView from '@/views/EditorView/EventsEditorView.vue';
import EventsUpdateView from '@/views/EditorView/EventsUpdateView.vue';
import ReceiveEventView from '@/views/ReceiveEventView.vue';
import store from '@/store';
import UnreadEvent from '@/views/ReceiveEvent/UnreadEvent.vue';
import ReadEvent from '@/views/ReceiveEvent/ReadEvent.vue';
import EventDetail from '@/views/ReceiveEvent/EventDetail.vue';

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: '/events/',
    name: 'events',
    component: EventsView,
    meta: {
        requestAuth: true,
    },
  },
  {
    path: '/EventsEditor/',
    name: 'EventsEditor',
    component: EventsEditorView,
    meta: {
        requestAuth: true,
    }
  },
  {
    path: '/EventsUpdate/:id',
    name: 'EventsUpdate',
    component: EventsUpdateView,
    meta: {
        requestAuth: true,
    }
  },
  {
    path: '/EventDetail/:id',
    name: 'EventDetail',
    component: EventDetail,
    meta: {
        requestAuth: true,
    }
  },
  {
    path: '/ReceiveEvent/',
    name: 'ReceiveEvent',
    component: ReceiveEventView,
    meta: {
      requestAuth: true,
    },
    children: [
      {
        path: 'UnreadEvent/',
        name: 'UnreadEvent',
        component: UnreadEvent,
        meta: {
          requestAuth: true,
        }
      },
      {
        path: 'ReadEvent/',
        name: 'ReadEvent',
        component: ReadEvent,
        meta: {
          requestAuth: true,
        }
      }
    ]
  },
  {
    path: '/RepairManage/',
    name: 'RepairManage',
    component: RepairManageView,
    meta: {
      requestAuth: true,
    },
    children: [
      {
        path: 'pending/',
        name: 'pending',
        component: PendingView,
        meta: {
          requestAuth: true,
        }
      },
      {
        path: 'processed/',
        name: 'processed',
        component: ProcessedView,
        meta: {
          requestAuth: true,
        }
      }
    ]
  },
  {
    path: '/StudentRepair/',
    name: 'StudentRepair',
    component: StudentRepairView,
    meta: {
      requestAuth: true,
    },
    children: [
      {
        path: 'RepairPending/',
        name: 'RepairPending',
        component: RepairPending,
        meta: {
          requestAuth: true,
        }
      },
      {
        path: 'RepairRevoke/',
        name: 'RepairRevoke',
        component: RepairRevoke,
        meta: {
          requestAuth: true,
        }
      },
      {
        path: 'RepairProcessed/',
        name: 'RepairProcessed',
        component: RepairProcessed,
        meta: {
          requestAuth: true,
        }
      }
    ]
  },
  {
    path: '/BuildingManage/',
    name: 'BuildingManage',
    component: BuildingManageView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: '/AddBuilding/',
    name: 'AddBuilding',
    component: AddBuilding,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: '/UpdateBuilding/:id',
    name: 'UpdateBuilding',
    component: UpdateBuilding,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: '/DormManage/',
    name: 'DormManage',
    component: DormManageView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: '/AddDorm/',
    name: 'AddDorm',
    component: AddDorm,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: '/UpdateDorm/:id',
    name: 'UpdateDorm',
    component: UpdateDorm,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: '/StudentManage/',
    name: 'StudentManage',
    component: StudentManageView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: '/userprofile/',
    name: 'userprofile',
    component: UserProfileView,
    meta: {
      requestAuth: true,
    }
  },
  {
    path: '/login/',
    name: 'login',
    component: LoginView,
    meta: {
      requestAuth: false,
    }
  },
  {
    path: '/register/',
    name: 'register',
    component: RegisterView,
    meta: {
      requestAuth: false,
    }
  },
  {
    path: '/404/',
    name: '404',
    component: NotFoundView,
    meta: {
      requestAuth: false,
    }
  },
  // 重定向
  {
    path: '/:catchAll(.*)',
    redirect: "/404/"
  }
]

const router = createRouter({
  history: createWebHistory(),  // 使用 HTML5 History 模式
  routes
})

// 每次使用router进入某一个页面之前，会先调用这个函数
// to表示跳转到哪个页面，from表示从哪个页面跳转过去的，next表示将页面要不要执行下一步操作
router.beforeEach((to, from, next) => {
  if (to.meta.requestAuth && !store.state.user.is_login) {
      next({name: "login"});
  } else {
      next();
  }
})

export default router
